package com.hotel.management.demo;

import com.hotel.management.dto.EmployeeDTO;
import com.hotel.management.dto.EmployeeQueryDTO;
import com.hotel.management.dto.PageResult;
import com.hotel.management.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工服务演示类
 * 演示Service层的各种功能
 * 
 * @author Hotel Management Team
 * @version 1.0.0
 * @since 2024-06-28
 */
@Component
public class EmployeeServiceDemo implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(EmployeeServiceDemo.class);

    private final EmployeeService employeeService;

    public EmployeeServiceDemo(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("=== 员工服务演示开始 ===");
        
        try {
            // 演示创建员工
            demonstrateCreateEmployee();
            
            // 演示查询员工
            demonstrateQueryEmployee();
            
            // 演示更新员工
            demonstrateUpdateEmployee();
            
            // 演示分页查询
            demonstratePageQuery();
            
            // 演示批量操作
            demonstrateBatchOperations();
            
            log.info("=== 员工服务演示完成 ===");
            
        } catch (Exception e) {
            log.error("演示过程中发生错误: {}", e.getMessage(), e);
        }
    }

    /**
     * 演示创建员工
     */
    private void demonstrateCreateEmployee() {
        log.info("--- 演示创建员工 ---");
        
        EmployeeDTO employeeDTO = new EmployeeDTO();
        employeeDTO.setName("张三");
        employeeDTO.setGender("男");
        employeeDTO.setAge(25);
        employeeDTO.setPhone("13800138001");
        employeeDTO.setEmail("zhangsan@hotel.com");
        employeeDTO.setHireDate(new Date());
        employeeDTO.setPosition("前台接待");
        employeeDTO.setRoleId(1L);
        employeeDTO.setStatus("在职");
        
        try {
            EmployeeDTO created = employeeService.createEmployee(employeeDTO);
            log.info("员工创建成功: ID={}, 姓名={}", created.getId(), created.getName());
        } catch (Exception e) {
            log.warn("员工创建失败: {}", e.getMessage());
        }
    }

    /**
     * 演示查询员工
     */
    private void demonstrateQueryEmployee() {
        log.info("--- 演示查询员工 ---");
        
        try {
            // 查询所有员工
            List<EmployeeDTO> allEmployees = employeeService.getAllEmployees();
            log.info("查询到所有员工数量: {}", allEmployees.size());
            
            if (!allEmployees.isEmpty()) {
                EmployeeDTO firstEmployee = allEmployees.get(0);
                log.info("第一个员工: ID={}, 姓名={}, 职位={}", 
                        firstEmployee.getId(), firstEmployee.getName(), firstEmployee.getPosition());
                
                // 根据ID查询
                EmployeeDTO byId = employeeService.getEmployeeById(firstEmployee.getId());
                log.info("根据ID查询员工: {}", byId.getName());
                
                // 根据姓名查询
                List<EmployeeDTO> byName = employeeService.getEmployeesByName(firstEmployee.getName());
                log.info("根据姓名查询到员工数量: {}", byName.size());
                
                // 根据职位查询
                List<EmployeeDTO> byPosition = employeeService.getEmployeesByPosition(firstEmployee.getPosition());
                log.info("根据职位查询到员工数量: {}", byPosition.size());
            }
            
            // 统计员工总数
            long totalCount = employeeService.countAllEmployees();
            log.info("员工总数: {}", totalCount);
            
        } catch (Exception e) {
            log.warn("查询员工失败: {}", e.getMessage());
        }
    }

    /**
     * 演示更新员工
     */
    private void demonstrateUpdateEmployee() {
        log.info("--- 演示更新员工 ---");
        
        try {
            List<EmployeeDTO> allEmployees = employeeService.getAllEmployees();
            if (!allEmployees.isEmpty()) {
                EmployeeDTO employee = allEmployees.get(0);
                Long employeeId = employee.getId();
                
                // 更新员工信息
                EmployeeDTO updateDTO = new EmployeeDTO();
                updateDTO.setPosition("高级前台接待");
                updateDTO.setAge(26);
                
                EmployeeDTO updated = employeeService.updateEmployee(employeeId, updateDTO);
                log.info("员工信息更新成功: ID={}, 新职位={}, 新年龄={}", 
                        updated.getId(), updated.getPosition(), updated.getAge());
                
                // 更新员工状态
                employeeService.updateEmployeeStatus(employeeId, "休假");
                log.info("员工状态更新成功: ID={}, 新状态=休假", employeeId);
            }
        } catch (Exception e) {
            log.warn("更新员工失败: {}", e.getMessage());
        }
    }

    /**
     * 演示分页查询
     */
    private void demonstratePageQuery() {
        log.info("--- 演示分页查询 ---");
        
        try {
            EmployeeQueryDTO queryDTO = new EmployeeQueryDTO();
            queryDTO.setPageNum(1);
            queryDTO.setPageSize(5);
            queryDTO.setSortBy("createTime");
            queryDTO.setSortDirection("DESC");
            
            PageResult<EmployeeDTO> pageResult = employeeService.getEmployeesWithPage(queryDTO);
            log.info("分页查询结果: 总数={}, 当前页={}, 每页大小={}, 总页数={}", 
                    pageResult.getTotal(), pageResult.getPageNum(), 
                    pageResult.getPageSize(), pageResult.getTotalPages());
            
            for (EmployeeDTO emp : pageResult.getRecords()) {
                log.info("  员工: ID={}, 姓名={}, 职位={}", emp.getId(), emp.getName(), emp.getPosition());
            }
        } catch (Exception e) {
            log.warn("分页查询失败: {}", e.getMessage());
        }
    }

    /**
     * 演示批量操作
     */
    private void demonstrateBatchOperations() {
        log.info("--- 演示批量操作 ---");
        
        try {
            // 批量创建员工
            EmployeeDTO emp1 = createTestEmployee("李四", "13800138002", "lisi@hotel.com", "客房服务");
            EmployeeDTO emp2 = createTestEmployee("王五", "13800138003", "wangwu@hotel.com", "餐厅服务");
            EmployeeDTO emp3 = createTestEmployee("赵六", "13800138004", "zhaoliu@hotel.com", "保安");
            
            List<EmployeeDTO> employees = Arrays.asList(emp1, emp2, emp3);
            
            int created = employeeService.batchCreateEmployees(employees);
            log.info("批量创建员工成功: 创建数量={}", created);
            
            // 查询新创建的员工ID
            List<EmployeeDTO> allEmployees = employeeService.getAllEmployees();
            List<Long> lastThreeIds = allEmployees.stream()
                    .skip(Math.max(0, allEmployees.size() - 3))
                    .map(EmployeeDTO::getId)
                    .collect(Collectors.toList());
            
            if (lastThreeIds.size() >= 2) {
                // 批量删除部分员工
                List<Long> idsToDelete = lastThreeIds.subList(0, 2);
                int deleted = employeeService.batchDeleteEmployees(idsToDelete);
                log.info("批量删除员工成功: 删除数量={}", deleted);
            }
            
        } catch (Exception e) {
            log.warn("批量操作失败: {}", e.getMessage());
        }
    }

    /**
     * 创建测试员工
     */
    private EmployeeDTO createTestEmployee(String name, String phone, String email, String position) {
        EmployeeDTO dto = new EmployeeDTO();
        dto.setName(name);
        dto.setGender("男");
        dto.setAge(28);
        dto.setPhone(phone);
        dto.setEmail(email);
        dto.setHireDate(new Date());
        dto.setPosition(position);
        dto.setRoleId(2L);
        dto.setStatus("在职");
        return dto;
    }
}
